#include "main.h"

/* 定义输入值 */
#define VALUE1	0XF1
#define VALUE2	0XF2
#define INVAKEY		0X00

#define PRINTER_DEV "/dev/print-io"

static int fd;
static pthread_t printer_io_id;
void *printer_io_ctrl_pth(void *arg)
{
	int keyvalue;
        while(SYSTEM_RUNNING_FALG)
        {
		read(fd, &keyvalue, sizeof(keyvalue));
		if (keyvalue == VALUE1)
                {
                        char msg_buffer[100];
                        snprintf (&msg_buffer[0],  sizeof(msg_buffer), "{\"%s\":\"%s\",\"exit-num\" :%d}", RABIITMQ_SENDER_ID, CMD_PRINTER_CTRL, reemoon_get_exit_id(1));
                        reemoon_msg_send(msg_buffer);
			reemoon_ignore ("input value = %#X\r\n", keyvalue);
		}
                else if (keyvalue == VALUE2 && system_info->exit_cnt == 2)
                {
                        char msg_buffer[100];
                        snprintf (&msg_buffer[0],  sizeof(msg_buffer), "{\"%s\":\"%s\",\"exit-num\" :%d}", RABIITMQ_SENDER_ID, CMD_PRINTER_CTRL, reemoon_get_exit_id(2));
                        reemoon_msg_send(msg_buffer);
			reemoon_ignore ("input value = %#X\r\n", keyvalue);
		}
		usleep(1000);
        }
}

int print_io_ctrl_init(void)
{
	/* 打开io驱动 */
	fd = open(PRINTER_DEV, O_RDWR);
	if(fd < 0){
		reemoon_error ("file %s open failed!\r\n", PRINTER_DEV);
		return -1;
	}

	pthread_create(&printer_io_id, NULL, printer_io_ctrl_pth, NULL);

	return 0;
}

void print_io_ctrl_deinit(void)
{
        if (printer_io_id)
                pthread_cancel(printer_io_id);

	int ret= close(fd); /* 关闭文件 */
	if(ret < 0){
		return;
	}
        reemoon_info ("print io deint\n");
}